﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Macds.Core.Log
{
    public class ObjectDebugManager
    {
        protected List<object> objs;

        public ObjectDebugManager()
        {
            this.objs = new List<object>();
        }

        public void Add(object obj)
        {
            this.objs.Add(obj);
        }

        public string GetInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("### LogObjectManager ###\r\n");
            foreach (object obj in objs)
            {
                if (obj != null)
                {
                    sb.Append(GetInfo(obj));
                }
            }

            return sb.ToString();
        }

        protected string GetInfo(object obj)
        {
            StringBuilder sb = new StringBuilder();

            string val = "";

            Type objType = obj.GetType();
            PropertyInfo[] props = objType.GetProperties();

            string name = objType.Name;

            if (IsSystemObject(obj))
            {
                val = obj.ToString();
                sb.Append(String.Format("{0}:{1}\r\n", name, val));
            }

            if (val == "")
            {
                if (name.ToLower().Trim() == typeof(ObjectContainer).Name.ToLower().Trim())
                {
                    PropertyInfo p1 = props[0];
                    PropertyInfo p2 = props[1];
                    sb.Append(string.Format("{0}:{1}\r\n", p1.GetValue(obj, null).ToString(), p2.GetValue(obj, null).ToString()));
                }
                else
                {
                    sb.Append(name + "\r\n");

                    foreach (PropertyInfo p in props)
                    {
                        if (p.CanRead)
                        {
                            string name1 = p.Name;
                            object data = p.GetValue(obj, null);

                            if (data != null)
                            {
                                if (IsSystemObject(data))
                                {
                                    sb.Append(String.Format("- {0}:{1}\r\n", name1, data.ToString()));
                                }
                                else
                                {
                                    sb.Append("### Children ###\r\n");
                                    sb.Append(GetInfo(data));
                                    sb.Append("### -------- ###\r\n");
                                }
                            }
                        }
                    }
                }
            }

            return sb.ToString();
        }

        protected bool IsSystemObject(object obj)
        {
            switch (obj.GetType().Name)
            {
                case "String":
                case "Int36":
                case "Int32":
                case "Int64":
                case "Double":
                case "float":
                case "Boolean":
                case "Decimal":
                case "DateTime":
                    return true;
                default:
                    if (obj.GetType().IsEnum)
                        return true;

                    return false;
            }
        }
    }
}
